<meta http-equiv="Content_Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="public/css/style.css">
<?php
require_once './model/db1.php';
$query = "SELECT * FROM apps";
$apps = getApps($query);
?>
<h2>Dinh Thi Thien Thu</h2>
<form method="POST">
    <table>
        <tr>
            <td>Min </td>
            <td><input type="text" name="min">
        </tr>
        <tr>
            <td>Max </td>
            <td><input type="text" name="max">
        </tr>
        <tr>
            <td>Apps </td>
            <td><select name="App">
                    <?php foreach ($apps as $app): ?>
                        <option value="<?php echo $app["id"] ?>">                     
                            <?php echo $app["name"] ?>                      
                        </option>
                    <?php endforeach; ?>
                </select>
        </tr>        
    </table>
    <input type="submit" name="submit" value="Search">
</form>
<?php
if (isset($_POST['submit'])) {
    $min = (int) $_POST['min'];
    $max = (int) $_POST['max'];
    $app = $_POST["App"];
    if (empty($min))
        $min = "";
    else
        $min = "(pages.likes>=$min) AND";
    if (empty($max))
        $max = "";
    else
        $max = "(pages.likes<=$max) AND";
    $app = "apps.id=$app";
    $query = "SELECT * FROM `pages`
               INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps_pages.id_app=apps.id
              WHERE 
                    $min
                    $max
                    $app
        limit 0,15";
}
else {
    $query = "SELECT * FROM pages limit 0,10";
}
var_dump($query);
$pages = getPages($query);
?>
<table boder="1">
    <tr>
        <td>Profile</td>
        <td>Likes</td>
        <td>Description</td>
    </tr>
    <?php foreach ($pages as $page): ?>

        <tr>
            <td><?php echo $page["picture"] ?></td>
            <td><?php echo $page["likes"] ?></td>
            <td><?php echo substr($page["description"],0,100); ?></td>
        </tr>
    <?php endforeach; ?>
</table>
